home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 5 / DINKDEMO / DINKCLAS / DSCROLLW.C < prev    next >
Text File  |  1992-07-23  |  11KB  |  554 lines

  1. /*
  2.     File:        DScrollWindow.c
  3.  
  4.     Written by:    Mark Gross
  5.  
  6.     Copyright:    ⌐ 1992 by Applied Technical Software, all rights reserved.
  7.     Use at your own risk.
  8.  
  9. */
  10.  
  11. // This is the implemntation of DScrollWindow class (aka DScrollWindow)
  12.  
  13. #include <DApplication.h>
  14. #include "DScrollWindow.h"
  15.  
  16.  
  17.  
  18. DScrollWindow::DScrollWindow(void)
  19. {
  20. //null
  21. }
  22.  
  23. DScrollWindow::~DScrollWindow(void)
  24. {
  25. //null
  26. }
  27.  
  28.         
  29. Boolean DScrollWindow::Init(DDocument *doc, Boolean hasColorWindows)
  30. {
  31.     Boolean inheritedSuccess;
  32.     Rect r;
  33.     
  34.     SetRect(&r, 0, 0, 0, 0);
  35.     inheritedSuccess = inherited::Init(doc, hasColorWindows);
  36.     
  37.     fHorizScrollBar = NULL;
  38.     fVertScrollBar = NULL;
  39.     fVMin = 0;
  40.     fVMax = 444;
  41.     
  42.     fHMin = 0;
  43.     fHMax = 444;
  44.     if (inheritedSuccess)
  45.     {
  46.         SetPort(fWindowPtr);
  47.         fHorizScrollBar = NewControl(fWindowPtr,&r, "",TRUE,
  48.                                 0, 0, 0, scrollBarProc, 0);
  49.         fVertScrollBar = NewControl(fWindowPtr,&r, "",TRUE,
  50.                                 0, 0, 0, scrollBarProc, 0);
  51.         
  52.         SizeScrollBars();
  53.     
  54.         SynchScrollBars();
  55.          InvalRect(&(fWindowPtr->portRect));
  56.      }
  57.     return ( (fHorizScrollBar != NULL)&&(fVertScrollBar != NULL) && inheritedSuccess);
  58. }// end of init function
  59.  
  60.  
  61.  
  62. void DScrollWindow::KillMeNext(void)
  63. {
  64.     KillControls(fWindowPtr);
  65.     inherited::KillMeNext();
  66. }
  67.  
  68.  
  69.  
  70.  
  71. void    DScrollWindow::HandleUpdateEvt(EventRecord *theEvent)
  72. {
  73.     Rect r;
  74.     
  75.     SetPort (fWindowPtr);
  76.     BeginUpdate(fWindowPtr);
  77.         
  78.     FocusOnContent();
  79.     r = (**(fWindowPtr->visRgn)).rgnBBox;
  80.     if( !EmptyRgn(fWindowPtr->visRgn) )
  81.         Draw(&r);
  82.         
  83.     FocusOnWindow();
  84.     DrawControls(fWindowPtr);
  85.     DrawGrowIcon( fWindowPtr);
  86.     EndUpdate(fWindowPtr);
  87.     
  88. }// end of HandleUpdateEvt member function
  89.  
  90.     
  91. void DScrollWindow::HandleActivateEvt(EventRecord *theEvent)
  92. {
  93.     Boolean    activating;
  94.     
  95.     activating = theEvent->modifiers & activeFlag;
  96.     if(activating)
  97.     {
  98.         FocusOnWindow();
  99.         ShowControl(fVertScrollBar);
  100.         ShowControl(fHorizScrollBar);
  101.         DrawControls(fWindowPtr);
  102.         DrawGrowIcon(fWindowPtr);
  103.  
  104.         FocusOnContent();
  105.         if( !EmptyRgn(fWindowPtr->visRgn) )
  106.             Draw( &(fWindowPtr->portRect));
  107.     }
  108.     else
  109.     {
  110.         FocusOnWindow();
  111.         HideControl(fVertScrollBar);
  112.         HideControl(fHorizScrollBar);
  113.     }
  114. }// end of HandleActivateEvt member function
  115.     
  116.  
  117.  
  118. void DScrollWindow::HandleOSEvent(EventRecord *theEvent)
  119. {
  120.     if (gApplication->fInBackground)
  121.     {
  122.         FocusOnWindow();
  123.         HideControl(fVertScrollBar);
  124.         HideControl(fHorizScrollBar);
  125.     }
  126.     else
  127.     {
  128.         FocusOnWindow();
  129.         ShowControl(fVertScrollBar);
  130.         ShowControl(fHorizScrollBar);
  131.         DrawControls(fWindowPtr);
  132.         DrawGrowIcon(fWindowPtr);
  133.  
  134.         FocusOnContent();
  135.     }
  136.     inherited::HandleOSEvent(theEvent);
  137.     
  138. }// end of HandleOSEvent member function
  139.  
  140.  
  141. short    DScrollWindow::GetVertLineScrollAmount(void)
  142. {
  143.     return 16;
  144. }//end of GetVertLineScrollAmount function
  145.  
  146.  
  147. short    DScrollWindow::GetHorizLineScrollAmount(void)
  148. {
  149.     return 16;
  150. }//end of GetHorizLineScrollAmount function
  151.  
  152.     
  153. short    DScrollWindow::GetVertPageScrollAmount(void)
  154. {
  155.     Rect r;
  156.     
  157.     GetContentRect(&r);
  158.     return r.bottom - r.top - kScrollOverlap;
  159. }//end of GetVertPageScrollAmount function
  160.  
  161.     
  162. short    DScrollWindow::GetHorizPageScrollAmount(void)
  163. {
  164.     Rect r;
  165.     
  166.     GetContentRect(&r);
  167.     return r.right - r.left - kScrollOverlap;
  168. }//end of GetHorizPageScrollAmount function
  169.  
  170.  
  171. void    DScrollWindow::DoGrow(EventRecord *theEvent)
  172. {
  173.     FocusOnWindow();
  174.     inherited::DoGrow(theEvent);
  175.     SizeScrollBars();
  176.     SynchScrollBars();
  177. }//end of DoGrow function
  178.  
  179.  
  180.  
  181.  
  182. void    DScrollWindow::DoZoom(short partCode)
  183. {
  184.     FocusOnWindow();
  185.     inherited::DoZoom(partCode);
  186.     SizeScrollBars();
  187.     SynchScrollBars();
  188. }//end of DoZoom function
  189.  
  190.  
  191.  
  192.  
  193. void    DScrollWindow::DoContent(EventRecord *theEvent)
  194. {
  195.     Rect contents;
  196.     
  197.     FocusOnWindow();
  198.     GlobalToLocal(&theEvent->where);
  199.     GetContentRect(&contents);
  200.     if(PtInRect(theEvent->where, &contents))
  201.     {
  202.         FocusOnContent();
  203.         inherited::DoContent(theEvent);
  204.     }
  205.     else
  206.         ScrollClick(theEvent);
  207. }// end of DoContent member function
  208.  
  209.     
  210.  
  211. void    DScrollWindow::SizeScrollBars()
  212. {
  213.     Rect r, r_temp;
  214.     
  215.     FocusOnWindow();
  216.     r = fWindowPtr->portRect;
  217.  
  218.     HideControl(fVertScrollBar);
  219.     SizeControl(fVertScrollBar, kScrollBarWidth,
  220.                 (r.bottom - r.top - kScrollBarPos) + 2);        
  221.     MoveControl(fVertScrollBar, r.right - kScrollBarPos, -1);
  222.     ShowControl(fVertScrollBar);
  223.     r_temp = (**fVertScrollBar).contrlRect;
  224.     ValidRect(&r_temp);
  225.     InvalRect(&r_temp);
  226.  
  227.     HideControl(fHorizScrollBar);
  228.     SizeControl(fHorizScrollBar, (r.right - r.left - kScrollBarPos) + 2, 
  229.                                     kScrollBarWidth);
  230.     MoveControl(fHorizScrollBar, -1, r.bottom - r.top - kScrollBarPos);
  231.     ShowControl(fHorizScrollBar);
  232.     r_temp = (**fHorizScrollBar).contrlRect;
  233.     ValidRect(&r_temp);
  234.     InvalRect(&r_temp);
  235.  
  236. }// end of SizeScroll Bars member fuction
  237.  
  238.  
  239.  
  240. void    DScrollWindow::ValidateScrollRange(void)
  241. {
  242.     Rect r;
  243.     short dh, dv;
  244.     short currentValue;
  245.     short newMax;
  246.     RgnHandle oldClip;
  247.     
  248.     GetContentRect(&r);
  249.     dh = 0; dv = 0;
  250.     newMax = (fVMax - fVMin) - (r.bottom - r.top); 
  251.     // see .h file for comment on control ranges vrs visible ranges
  252.         
  253.     if(newMax < 0)
  254.         newMax = 0;
  255.     if(fVOffSet > newMax)
  256.         dv = fVOffSet - newMax;
  257.     SetCtlMax(fVertScrollBar, newMax);
  258.  
  259.     newMax = (fHMax - fHMin) - (r.right - r.left);
  260.     if(newMax < 0)
  261.         newMax = 0;
  262.     if(fHOffSet > newMax)
  263.         dh = fHOffSet - newMax;
  264.     SetCtlMax(fHorizScrollBar, newMax);
  265.     
  266.     if(dh | dv)
  267.     {
  268.         FocusOnContent();
  269.         
  270.         GetContentRect(&r);
  271.         InvalRect(&r);
  272.         
  273.         oldClip = NewRgn();
  274.         GetClip(oldClip);
  275.         SetRect(&r, 0, 0, 0, 0);
  276.         ClipRect(&r);
  277.         ScrollContents(dh,dv);
  278.         
  279.         SetClip(oldClip);
  280.         DisposeRgn(oldClip);
  281.     }
  282. }// end of ValidateScrollRange member function
  283.  
  284.  
  285. void    DScrollWindow::ValidateVertScrollRange(void)
  286. {
  287.     Rect r;
  288.     short dv;
  289.     short currentValue;
  290.     short newMax;
  291.     RgnHandle oldClip;
  292.     
  293.     GetContentRect(&r);
  294.     dv = 0;
  295.     newMax = (fVMax - fVMin) - (r.bottom - r.top);
  296.     if(newMax < 0)
  297.         newMax = 0;
  298.     if(fVOffSet > newMax)
  299.         dv = fVOffSet - newMax;
  300.     SetCtlMax(fVertScrollBar, newMax);
  301.     
  302.     if(dv)
  303.     {
  304.         FocusOnContent();
  305.         
  306.         GetContentRect(&r);
  307.         InvalRect(&r);
  308.         
  309.         oldClip = NewRgn();
  310.         GetClip(oldClip);
  311.         SetRect(&r, 0, 0, 0, 0);
  312.         ClipRect(&r);
  313.         ScrollContents(0,dv);
  314.         
  315.         SetClip(oldClip);
  316.         DisposeRgn(oldClip);
  317.     }
  318. }// end of ValidateVertScrollRange member function
  319.  
  320.  
  321.  
  322. void    DScrollWindow::SynchScrollBars(void)
  323. {
  324.     ValidateScrollRange();
  325.     FocusOnWindow();
  326.     SetCtlValue(fHorizScrollBar, fHOffSet);
  327.     SetCtlValue(fVertScrollBar, fVOffSet);
  328.     
  329. }// end of SynchScrollBars member function
  330.  
  331.  
  332.  
  333.  
  334. void    DScrollWindow::ScrollClick(EventRecord *theEvent)
  335. {
  336.     ControlHandle whichControl;
  337.     short part;
  338.     
  339.     FocusOnWindow();
  340.     if(part = FindControl(theEvent->where, fWindowPtr, &whichControl) )
  341.     {
  342.         switch(part)
  343.         {
  344.             case inThumb:
  345.                 DoThumbScroll(whichControl, theEvent->where);
  346.                 break;
  347.             case inUpButton:
  348.             case inDownButton:
  349.                 DoButtonScroll(whichControl, theEvent->where);
  350.                 break;
  351.             case inPageUp:
  352.             case inPageDown:
  353.                 DoPageScroll(whichControl,part);
  354.                 break;
  355.         }/*end of switch*/
  356.     }
  357. }// end of ScrollClick member function
  358.  
  359.     
  360. void    DScrollWindow::DoThumbScroll(ControlHandle theControl, Point localPt)
  361. {
  362.     short oldValue, trackResult, newValue, diff;
  363.     
  364.     oldValue = GetCtlValue(theControl);
  365.     trackResult = TrackControl(theControl, localPt, NULL);
  366.     if(trackResult != 0)
  367.     {
  368.         newValue = GetCtlValue(theControl);
  369.         diff = newValue - oldValue;
  370.  
  371.         if(theControl == fHorizScrollBar)
  372.             DoHScroll(diff);
  373.         if(theControl == fVertScrollBar)
  374.             DoVScroll(diff);
  375.     }
  376. }// end of DoThumbScroll member function
  377.  
  378.     
  379.  
  380.  
  381. void    DScrollWindow::DoPageScroll(ControlHandle theControl, short part)
  382. {
  383.     short scrollAmount, currentPart;
  384.     Point thePt;
  385.     
  386.     if(theControl == fVertScrollBar)
  387.     {
  388.         scrollAmount = GetVertPageScrollAmount();
  389.         do
  390.         {
  391.             GetMouse(&thePt);
  392.             currentPart = TestControl(theControl, thePt);
  393.             if(currentPart == part)
  394.             {
  395.                 if(currentPart == inPageUp)
  396.                     DoVScroll(- scrollAmount);
  397.                 if(currentPart == inPageDown)
  398.                     DoVScroll(scrollAmount);
  399.             }
  400.         }while(Button());
  401.     }// end if VeriticalScrollBar
  402.     else if (theControl == fHorizScrollBar)
  403.     {
  404.         scrollAmount = GetHorizPageScrollAmount();
  405.         do
  406.         {
  407.             GetMouse(&thePt);
  408.             currentPart = TestControl(theControl, thePt);
  409.             if(currentPart == part)
  410.             {
  411.                 if(currentPart == inPageUp)
  412.                     DoHScroll(- scrollAmount);
  413.                 if(currentPart == inPageDown)
  414.                     DoHScroll(scrollAmount);
  415.             }
  416.         }while(Button());
  417.     }// end ifHorizontal Scroll Bar
  418. }// end of DoPageScroll function
  419.  
  420.  
  421.  
  422. void    DScrollWindow::DoButtonScroll(ControlHandle theControl, Point localPt)
  423. {
  424.     short result;
  425.     
  426.     result = TrackControl(theControl, localPt, (ProcPtr) ActionProc) ;
  427. }// end of DoButtonScroll member function
  428.  
  429.  
  430.  
  431.     
  432. void    DScrollWindow::ScrollContents(short dh, short dv)
  433. {
  434.     Rect r;
  435.     RgnHandle updateRgn;
  436.     
  437.     GetContentRect(&r);
  438.     updateRgn = NewRgn();
  439.     
  440.     ScrollRect(&r,dh,dv,updateRgn);
  441.     
  442.     fVOffSet -= dv;
  443.     fHOffSet -= dh;
  444.     
  445.     InvalRgn(updateRgn);
  446.     HandleUpdateEvt(NULL);
  447.     DisposeRgn(updateRgn);
  448. }// end of ScrollContents function
  449.  
  450.  
  451.  
  452. void    DScrollWindow::DoHScroll( short change)
  453. {
  454.     RgnHandle oldClip;
  455.     short diff;
  456.     short newValue;
  457.     short oldMin, oldMax, oldCurrent;
  458.  
  459.     oldClip = NewRgn();
  460.     GetClip(oldClip);
  461.     diff = 0;
  462.     newValue = fHOffSet + change;
  463.     
  464.     if(change<0)
  465.     {
  466.         oldMin = GetCtlMin(fHorizScrollBar);
  467.         if(newValue < oldMin)
  468.             newValue = oldMin;
  469.     }
  470.     else
  471.     {
  472.         oldMax = GetCtlMax(fHorizScrollBar);
  473.         if(newValue > oldMax)
  474.             newValue = oldMax;
  475.     }
  476.     diff = fHOffSet - newValue;
  477.     
  478.     FocusOnContent();
  479.     ScrollContents(diff,0);
  480.     
  481.     FocusOnWindow();
  482.     SetCtlValue(fHorizScrollBar, fHOffSet);
  483.     
  484.     SetClip(oldClip);
  485.     DisposeRgn(oldClip);
  486. }// end of DoHScroll member function
  487.  
  488.  
  489. void    DScrollWindow::DoVScroll(short change)
  490. {
  491.     RgnHandle oldClip;
  492.     short diff;
  493.     short newValue;
  494.     short oldMin, oldMax, oldCurrent;
  495.     
  496.     oldClip = NewRgn();
  497.     GetClip(oldClip);
  498.     diff = 0;
  499.  
  500.     newValue = fVOffSet + change;
  501.     
  502.     if(change<0)
  503.     {
  504.         oldMin = GetCtlMin(fVertScrollBar);
  505.         if(newValue < oldMin)
  506.             newValue = oldMin;
  507.     }
  508.     else
  509.     {
  510.         oldMax = GetCtlMax(fVertScrollBar);
  511.         if(newValue > oldMax)
  512.             newValue = oldMax;
  513.     }
  514.     diff = fVOffSet - newValue;
  515.     
  516.     FocusOnContent();
  517.     ScrollContents(0, diff);
  518.     
  519.     FocusOnWindow();
  520.     SetCtlValue(fVertScrollBar, fVOffSet);
  521.  
  522.     
  523.     SetClip(oldClip);
  524.     DisposeRgn(oldClip);
  525. }// DoVScroll member function
  526.  
  527.  
  528.     
  529. pascal void DScrollWindow::ActionProc(ControlHandle theControl, short partCode)
  530. {
  531.     short scrollAmount = 0;
  532.     DScrollWindow *theCurrScrollWindow;
  533.     
  534.     theCurrScrollWindow = (DScrollWindow *) GetWRefCon( FrontWindow() );// isn't system7 great?
  535.     
  536.     if(theControl == theCurrScrollWindow->fVertScrollBar )
  537.     {
  538.         scrollAmount = theCurrScrollWindow->GetVertLineScrollAmount();
  539.         if(partCode == inUpButton)
  540.             theCurrScrollWindow->DoVScroll(-scrollAmount);
  541.         if(partCode == inDownButton)
  542.             theCurrScrollWindow->DoVScroll(scrollAmount);
  543.     }
  544.     else if( theControl == theCurrScrollWindow->fHorizScrollBar)
  545.     {
  546.         scrollAmount = theCurrScrollWindow->GetHorizLineScrollAmount();
  547.         if(partCode == inUpButton)
  548.             theCurrScrollWindow->DoHScroll(-scrollAmount);
  549.         if(partCode == inDownButton)
  550.             theCurrScrollWindow->DoHScroll(scrollAmount);
  551.     }
  552.  
  553. }// end of ActionProc proc
  554.